package com.yiren.dbaa.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.LightCombinationEntity;
import com.yiren.dbaa.R;

import java.util.List;


public class GroupLightNameAdapter extends BaseAdapter {

    private Context context;
    private List<LightCombinationEntity> list;
    public int mSelect = 0;

    public GroupLightNameAdapter(Context context, List<LightCombinationEntity> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void remove(int arg0) {// 删除指定位置的item
        list.remove(arg0);
        this.notifyDataSetChanged();// 不要忘记更改适配器对象的数据源
    }

    public void insert(LightCombinationEntity item, int arg0) {// 在指定位置插入item
        list.add(arg0, item);
        this.notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (null == convertView) {
            convertView = View.inflate(context, R.layout.list_grouplight, null);
            viewHolder = new ViewHolder();
            viewHolder.tv_grouplight_name = (TextView) convertView.findViewById(R.id.tv_grouplight_name);
            viewHolder.rl_grouplight = (RelativeLayout) convertView.findViewById(R.id.rl_grouplight);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv_grouplight_name.setText("灯光" + (position + 1));
        if (mSelect == position) {
            viewHolder.tv_grouplight_name.setTextColor(context.getResources().getColor(R.color.white));
            viewHolder.tv_grouplight_name.setBackground(context.getResources().getDrawable(R.drawable.btn_blue_radius_selector));
        }else {
            viewHolder.tv_grouplight_name.setTextColor(context.getResources().getColor(R.color.blue_select_text));
            viewHolder.tv_grouplight_name.setBackground(context.getResources().getDrawable(R.drawable.bg_blue_half));

        }

        viewHolder.rl_grouplight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(v, position);
                }

            }
        });
        return convertView;
    }

    public class ViewHolder {
        private TextView tv_grouplight_name;//灯光组合名字
        private RelativeLayout rl_grouplight;
    }

    public void changeSelected(int positon) {
        //刷新方法
        if (positon != mSelect) {
            mSelect = positon;
            notifyDataSetChanged();
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }


    public void reflesh(List<LightCombinationEntity> lights) {
        this.list.clear();
        this.list.addAll(lights);
        //重点内容
        this.notifyDataSetChanged();
    }


}
